package com.woniuxy.oa.web;

import com.woniuxy.oa.entity.Permission;
import com.woniuxy.oa.entity.ResponseVO;
import com.woniuxy.oa.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: M
 * @Date: 2019/12/28 09:40
 * @Description:
 */
@CrossOrigin("*")
@Controller
@RequestMapping("perm")
public class PermissionController {
    @Autowired
    private PermissionService permService;
    @ResponseBody
    @GetMapping("getAll")
    public ResponseVO getAll(){
        List<Permission> perms=permService.getPermissions();
        List<Permission> sons=new ArrayList<>();
        List<Permission> parents=new ArrayList<>();
        for(Permission perm:perms){
            if(perm.getLevelNum()==1){
                parents.add(perm);
            }
        }
        for(Permission parent:parents){
            for(Permission perm :perms){
                if(perm.getLevelNum()==2&&perm.getParentId()==parent.getId()){
                    sons.add(perm);
                }
            }
            parent.setPermissionList(sons);
        }
        ResponseVO responseVO=new ResponseVO();
        responseVO.setCode("200");
        responseVO.setData(parents);
        return  responseVO;
    }
    @GetMapping("all/{id}")
    @ResponseBody
    public ResponseVO responseVO(@PathVariable int id){
        ResponseVO responseVO=new ResponseVO();
        List<Permission> deptPerms=permService.getPermissionById(id);
        responseVO.setCode("200");
        responseVO.setData(deptPerms);
        return responseVO;
    }
}
